<!--
 * @Author: HaiF
 * @Date: 2020-11-30 22:43:38
 * @LastEditTime: 2021-02-25 15:52:08
 * @LastEditors: Please set LastEditors
 * @Description: 角色新增组件
 * @FilePath: \adminPC\src\views\role\widget\created.vue
 * @可以输入预定的版权声明、个性签名、空行等
-->

<template>
  <div class="wrapper">
    <JForm :form-config="dynamicValidateForm" :read="read" @handleBoxEvent="handleBoxEvent" />
  </div>
</template>

<script>
import JForm from '@/components/JForm'
import { getDictListByType } from '@/api/dictionaries'
export default {
  name: 'TenantCreated',
  components: { JForm },
  props: {
    emitData: {
      type: Object,
      default: () => ({})
    },
    read: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const userInfo = [
      {
        name: 'name',
        label: '角色',
        type: 'string',
        required: true
      },
      {
        name: 'alias',
        label: '别名',
        type: 'string',
        required: false
      },
      {
        name: 'iconUrl',
        label: '图标',
        type: 'string',
        required: false,
        config: {
          icon: true
        }
      },
      {
        name: 'isSysRole',
        label: '系统角色',
        type: 'option',
        required: false,
        config: {}
      },
      {
        name: 'remark',
        label: '备注',
        type: 'string',
        required: false,
        placeholder: ''
      },
      {
        name: 'tenantCode',
        label: '租户编码',
        type: 'string',
        required: false,
        placeholder: ''
      }
    ]
    return {
      userInfo,
      YN01: []
    }
  },
  computed: {
    dynamicValidateForm() {
      const data = this.userInfo
      const result = []
      data.forEach(i => {
        const { type } = i
        if (type === 'option') {
          i.config.options = this.filterOption(i)
        }
        i.value = ''
        result.push(i)
      })
      if (this.emitData) {
        result.forEach(i => {
          const { name } = i
          i.value = this.emitData[name]
        })
      }
      return result
    },
    isSysRoleOption() {
      const data = this.YN01
      data.forEach(item => {
        const { code, name } = item
        item.value = +code
        item.label = name
      })
      return data
    }
  },
  watch: {},
  created() {
    this.getDicYN01()
  },
  mounted() {},
  methods: {
    getDicYN01() {
      getDictListByType({ dictTypeCode: 'YN01' }).then(res => {
        const { code, data /* message */ } = res
        if (code === 200) {
          this.YN01 = data
        }
      })
    },
    handleBoxEvent(emitObj) {
      this.submitForm(emitObj)
    },
    filterOption(item) {
      const key = item.name
      return this[`${key}Option`]
    },
    submitForm(emitObj) {
      const target = emitObj
      this.$emit('handleBoxEvent', { target })
    }
  }
}
</script>
<style lang="scss" scoped>
.wrapper{
   padding: 0px;
   margin: 0px;
}
</style>

